package com.framework.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.app.entity.AppMember;
import com.framework.app.entity.dto.AppMemberQueryDTO;
import com.framework.app.entity.vo.AppMemberListVO;
import com.framework.app.entity.vo.AppMemberVO;
import com.framework.app.mapper.AppMemberMapper;
import com.framework.app.service.IAppMemberService;
import com.framework.core.util.StpAppUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Kevin
 * @since 2022-05-03
 */
@Service
@AllArgsConstructor
public class AppMemberServiceImpl implements IAppMemberService {

    private AppMemberMapper appMemberMapper;

    @Override
    public Page<AppMemberListVO> selectAppMemberWithPage(Page reqPage, AppMemberQueryDTO req) {
        LambdaQueryWrapper<AppMember> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StrUtil.isNotBlank(req.getPhone()), AppMember::getPhone, req.getPhone());
        IPage<AppMember> page = appMemberMapper.selectPage(reqPage, queryWrapper);
        Page<AppMemberListVO> voPage = BeanUtil.copyProperties(page, Page.class);
        voPage.setRecords(BeanUtil.copyToList(page.getRecords(), AppMemberListVO.class));
        return voPage;
    }

    @Override
    public AppMemberVO getUserInfo() {
        AppMember appUser = appMemberMapper.selectById(StpAppUtil.getLoginIdAsLong());
        return BeanUtil.copyProperties(appUser, AppMemberVO.class);
    }

}
